﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewMintesOfAMeetingOfACommittee : System.Web.UI.Page
    {
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 10;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {

            MemberAL memberAL = new MemberAL();
            string committeeId = Request.QueryString.ToString();
            List<MinutesDTO> allMinutes;
            List<MinutesDTO> allMinutesOfOnePage = new List<MinutesDTO>();
            string meetingId;
            if (committeeId != "")
            {
                allMinutes = memberAL.GetAllMinutesByCommitteeId(new Guid(committeeId)).OrderByDescending(a => a.HeldOn).OrderBy(b=>b.MinutesTitle).ToList();
                //gvAllMinutes.DataSource = allMinutes;
                //gvAllMinutes.DataBind();

                if (allMinutes.Count == 0)
                {
                    //If you didn't get any minutes for this committee that means the url is being issues is not "viewAllMintes.aspx?" + committeeId;
                    //You are issuing "viewAllMintes.aspx?" + meetingId; Hence you have to call methods accordingly
                    meetingId = committeeId;
                    allMinutes = memberAL.GetAllMinutesOfMeeting(new Guid(meetingId)).OrderByDescending(a => a.HeldOn).OrderBy(b=>b.MinutesTitle).ToList();

                    //gvAllMinutes.DataSource = allMinutes;
                    //gvAllMinutes.DataBind();
                }
            }
            else
            {
                allMinutes = memberAL.GetAllMinutes().OrderByDescending(a => a.HeldOn).OrderBy(b=>b.MinutesTitle).ToList();
                //gvAllMinutes.DataSource = allMinutes;
                //gvAllMinutes.DataBind();
            }


            // get the total rows 
            double totalRows = (int)allMinutes.Count;

            lblAllMinPage.Text = "Displaying Minute(s) -  Page ";
            lblAllMinOf.Text = "  of  ";
            lblAllMinTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllMinCurrPage.Text = currentPageNumber.ToString();

            int totalPageNumber = lblAllMinTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllMinTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllMinFirst.Enabled = false;
                    btnAllMinPrev.Enabled = false;
                    btnAllMinNext.Enabled = false;
                    btnAllMinLast.Enabled = false;
                    allMinutesOfOnePage = allMinutes;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllMinFirst.Enabled = false;
                    btnAllMinPrev.Enabled = false;
                    btnAllMinNext.Enabled = true;
                    btnAllMinLast.Enabled = true;
                    allMinutesOfOnePage = allMinutes.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllMinFirst.Enabled = true;
                    btnAllMinPrev.Enabled = true;
                    btnAllMinNext.Enabled = true;
                    btnAllMinLast.Enabled = true;
                    allMinutesOfOnePage = allMinutes.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllMinFirst.Enabled = true;
                    btnAllMinPrev.Enabled = true;
                    btnAllMinNext.Enabled = false;
                    btnAllMinLast.Enabled = false;
                    allMinutesOfOnePage = allMinutes.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allMinutes.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }

            
            gvAllMinutes.DataSource = allMinutesOfOnePage;
            gvAllMinutes.DataBind();


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllMinCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllMinCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllMinTotalPage.Text);
                    break;
            }


            BindData();
        }
    }
}
